package com.cangsan.erhai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cangsan.erhai.common.PageBean;
import com.cangsan.erhai.common.R;
import com.cangsan.erhai.entity.SysDictData;
import com.cangsan.erhai.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/dict")
public class SysDictController {

    @Autowired
    private SysDictDataService serviceData;

    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("dict_label",pageBean.getQuery());
        queryWrapper.orderByDesc( "dict_type","dict_sort");
        Page<SysDictData> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<SysDictData> pageResult = serviceData.page(page,queryWrapper);
        Map<String,Object> result = new HashMap<>();
        List<SysDictData> records = pageResult.getRecords();
        result.put("dataList",records);
        result.put("total",pageResult.getTotal());
        return R.ok(result);
    }

    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody SysDictData sysDictData){
        serviceData.saveOrUpdate(sysDictData);
        return R.ok();
    }

    @GetMapping("/getListByType/{dicType}")
    public R getListByType(@PathVariable String dicType){
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_type",dicType);
        queryWrapper.orderByAsc(true,"dict_sort");

        return R.ok(serviceData.list(queryWrapper));
    }


    @GetMapping("/{id}")
    public R delete(@PathVariable Long id) {
        serviceData.removeById(id);
        return R.ok("删除成功");
    }

}
